In the Write command button, you begin by dimming a variable named oFile as a TextStream class. This instantiates a new object based on the TextStream class. You can now use the properties and methods of this object to write the data to the text file. Heres the code:
Private Sub cmdWrite_Click() Dim oFile As TextStream Dim boolRet As Boolean
Set oFile = New TextStream
With oFile
If .OpenTextFile(txtFile.Text, ForAppending) Then
Call .WriteLine(txtEdit)
Else
MsgBox "Error opening text file"
End If
.CloseFile
End With
Set oFile = Nothing
End Sub
The OpenTextFile() method is passed in the name of the file and the mode in which to open the file. If the file does not exist, it will be created. If theres an error in creating the file, a False value will be returned from the method, and you can then perform appropriate error handling. After the file is open, call the WriteLine() method, passing in the text that you want to store in the file.
Lets take a look at the OpenTextFile() method to see how it works:
Public Function OpenTextFile(strFile As String, _
Optional intOpenMode As TSOpenMode = ForReading, _
Optional intFormat As TSFormat = TristateTrue) _
As Boolean
If strFile <> "" Then
mstrFileName = strFile
mboolEOF = False
mintOpenMode = intOpenMode
mintFormat = intFormat